package com.example.main;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.example.main.utils.Base64ImageUtils;
import com.example.main.utils.HeaderByteUtils;
import com.litesuits.common.io.FileUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SaveMultiFormatActivity extends Activity {
    /******
     * byte 头数量
     * 1 byte 类型
     * 2~5大小
     * 6~9文件字节起始位置
     */

    private Button btn_multi;
    private Button btn_parse_header;
    private ImageView iv_header;
    public static final String path = Environment.getExternalStorageDirectory() + File.separator + "aaa" + File.separator + "ccc";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.save_multi_format_file);
        iv_header = findViewById(R.id.iv_header);
        btn_parse_header = findViewById(R.id.btn_parse_header);
        btn_parse_header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    List<int[]> headerInfoList = HeaderByteUtils.parseHeaderItem(new File(path));
                    byte[] content = FileUtils.readFileToByteArray(new File(path));

                    List<byte[]> jsonByteList = HeaderByteUtils.parseForType(HeaderByteUtils.TYPE_JSON, headerInfoList, content);
                    for (int i = 0; i < jsonByteList.size(); i++) {
                        String jsonValue = new String(jsonByteList.get(i), StandardCharsets.UTF_8);
                        JSONObject jsonObject = new JSONObject(jsonValue);
                    }

                    List<byte[]> imageByteList = HeaderByteUtils.parseForType(HeaderByteUtils.TYPE_IMAGE, headerInfoList, content);
                    for (int i = 0; i < imageByteList.size(); i++) {
                        String bitmap64Value = new String(imageByteList.get(i));
                        Bitmap bitmap = Base64ImageUtils.base64ToBitmap(bitmap64Value);
                        iv_header.setImageBitmap(bitmap);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        btn_multi = findViewById(R.id.btn_multi);
        btn_multi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {

                    List<HeaderByteUtils.ByteContentBean> byteContentBeanList = new ArrayList<>();
                    //Json数据
                    HashMap<Integer, byte[]> hashMap = new HashMap<>();
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("ding", "中文");
                    byteContentBeanList.add(new HeaderByteUtils.ByteContentBean(HeaderByteUtils.TYPE_JSON, jsonObject.toString().getBytes(StandardCharsets.UTF_8)));

                    jsonObject = new JSONObject();
                    jsonObject.put("cccc", "english");
                    hashMap.put(HeaderByteUtils.TYPE_JSON, jsonObject.toString().getBytes(StandardCharsets.UTF_8));
                    byteContentBeanList.add(new HeaderByteUtils.ByteContentBean(HeaderByteUtils.TYPE_JSON, jsonObject.toString().getBytes(StandardCharsets.UTF_8)));

                    //图片
                    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                    String value = Base64ImageUtils.bitmapToBase64(bitmap);
                    byteContentBeanList.add(new HeaderByteUtils.ByteContentBean(HeaderByteUtils.TYPE_IMAGE, value.getBytes()));

                    FileUtils.writeByteArrayToFile(new File(path), HeaderByteUtils.addFullContent(byteContentBeanList));
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
